<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/6 0006
 * Time: 16:37
 */

namespace app\wap\controller;


use think\Db;

class Beili extends Middle
{

    //todo 背离提醒列表
    public function BeiLiMindList(){
        $array = ['btc','eth','bch','ltc','eos','xrp'];      	
        $resse = [];
      	
        foreach ($array as $k=>$v){
          	//var_dump($v);die;
            $map = ['uid'=>$this->uid,'title'=>$v];
            $user = Db::name('beili_user')
                ->where($map)
                ->field('title,sum(time) as time')
                ->group('title')
                ->find();
            $time = '';
            $x = 0;
            switch ($user['time']){
                case 29;
                    $time = '1,4,24';
                    break;
                case 25;
                    $time = '1,24';
                    break;
                case 5;
                    $time = '1,4';
                    break;
                case 1;
                    $time = '1';
                    break;
                case 4;
                    $time = '4';
                    break;
                case 24;
                    $time = '24';
                    break;
            }
          	$price = Db::name('jiage')->where(['platform'=>'Huobi','title'=>$v])->value('price');
          	$nowprice =  Db::name('timelydata')->where(['platform'=>'Huobi','title'=>$v])->value('price');
            if($time)$x = 1;
            	$resse[] = [
                'time' =>$time,
                'title'=>strtoupper($v),
                'price'=>$price,
                'CNY'  =>$this->ChangeCNY($price),
                'lv'   =>($nowprice - $price)/$price,
                'x'    =>$x,
                'times'=>date('m-d H:i',time()),
             ];
        }
        return $this->returnSuccess($resse);
    }

    //todo 背离提醒设置
    public function BeiLiMindSet(){
      	if($this->vip == 0)return $this->returnError('请先订阅会员服务');
      	if($this->vip != 2 && $this->vip_expire < time())return $this->returnError('服务已到期,请续费');
        $map = [
            'uid'   =>$this->uid,
            'title' =>$this->data['title'],
        ];
        $res = Db::name('beili_user')->where($map)->find();
        $time = $this->data['time'];
        if($res && $time == $res['time'])return $this->returnError('已经开启该项提醒');
        $map['time'] = $time;
        $ref = Db::name('beili_user')->insert($map);
        if($ref)return $this->returnSuccess(null,'成功订阅');
        return $this->returnError($this->Error);
    }

    //todo 背离提醒删除
    public function BeiLiMindDel(){
        $map = [
            'uid'   =>$this->uid,
            'title' =>$this->data['title'],
            'time'  =>['in',explode(',',$this->data['time'])],
        ];
        $res = Db::name('beili_user')->where($map)->find();
        if(!$res)return $this->returnError('不存在该项提醒');
        $ref = Db::name('beili_user')->where($map)->delete();
        if($ref)return $this->returnSuccess(null,'取消订阅');
        return $this->returnError($this->Error);
    }
}